/*   Copyright 2004 BEA Systems, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package wx.xml.bea.xml.stream.events;

import java.util.List;
import java.util.Locale;

import wx.xml.stax.api.stream.events.DTD;
import wx.xml.stax.api.stream.events.EntityDeclaration;
import wx.xml.stax.api.stream.events.NotationDeclaration;
import wx.xml.stax.api.stream.events.XMLEvent;
import wx.xml.wutka.dtd.DTDEntity;
import wx.xml.wutka.dtd.DTDExternalID;
import wx.xml.wutka.dtd.DTDNotation;
import wx.xml.wutka.dtd.DTDPublic;

public class DTDEvent
    extends BaseEvent
    implements DTD {
    private String dtd;

    private List notations;
    private List entities;

    public DTDEvent() {
        init();
    }

    public DTDEvent(String dtd) {
        init();
        setDTD(dtd);
    }

    public static EntityDeclaration createEntityDeclaration(DTDEntity dtdEntity) {
        return new EntityDeclarationEvent(dtdEntity.getName(), dtdEntity.getValue());
    }

    public static NotationDeclaration createNotationDeclaration(DTDNotation dtdNotation) {
        DTDExternalID extId    = dtdNotation.getExternalID();
        String        systemId = extId.getSystem();
        String publicId = (extId instanceof DTDPublic) ?
                          ((DTDPublic) extId).getPub() : null;

        return new NotationDeclarationEvent(dtdNotation.getName(), publicId, systemId);
    }

    protected void init() {
        setEventType(XMLEvent.DTD);
    }

    public void setDTD(String dtd) {
        this.dtd = dtd;
    }

    public Object getProcessedDTD() {
        return null;
    }

    public String getDocumentTypeDeclaration() {
        return dtd;
    }

    public List getEntities() {
        return entities;
    }

    public void setEntities(List l) {
        entities = l;
    }

    public List getNotations() {
        return notations;
    }

    public void setNotations(List l) {
        notations = l;
    }

    protected void doWriteAsEncodedUnicode(java.io.Writer writer)
        throws java.io.IOException {
        writer.write("<!DOCTYPE ");
        // !!! TBI: Should get the root element name here...
        if (dtd != null && dtd.length() > 0) {
            writer.write('[');
            writer.write(dtd);
            writer.write(']');
        }
        writer.write('>');
    }

    @Override
    public String debugDescription() {
        return String.format(Locale.getDefault(), "%d:%d", line(), column());
    }
}
